package com.tuyan.aixiang.component;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.tuyan.aixiang.R;
import com.tuyan.aixiang.common.Constant;
import com.tuyan.aixiang.common.lang.Consumer;
import com.tuyan.aixiang.model.ShortCut;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;

public class ControlPanel extends LinearLayout {

    @BindView(R.id.ll_max_container)
    LinearLayout llMaxContainer;

    @BindView(R.id.col_1)
    LinearLayout col1;

    @BindView(R.id.col_2)
    LinearLayout col2;

    private Consumer<ShortCut> consumer;

    List<ControlShortcut> cshortcuts = new ArrayList<>();

    public ControlPanel(Context context) {
        this(context, null);
    }

    public ControlPanel(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ControlPanel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View contentView = LayoutInflater.from(context).inflate(R.layout.control_panel, this);
        ButterKnife.bind(contentView, this);
    }

    public void setup(List<ShortCut> shortCusList) {
        llMaxContainer.removeAllViews();
        col1.removeAllViews();
        col2.removeAllViews();
        cshortcuts.clear();

        ShortCut more = new ShortCut();
        more.setBgColor("#FFFFFF");
        more.setTitleRes(R.string.shortcut_more);
        more.setIconRes(R.drawable.icon_more);
        more.setUrl(Constant.SHORTCUT_MORE);


        shortCusList.add(more);

        for (int i = 0; i < shortCusList.size(); i++) {
            ShortCut shortCut = shortCusList.get(i);

            ControlShortcut controlShortCut = new ControlShortcut(getContext()).setup(shortCut.getIconRes(), shortCut.getTitleRes(), shortCut.getBgColor());
            controlShortCut.setShowMsgCount(shortCut.isShowMsgCount());
//            controlShortCut.badge(shortCut.getCount());
            if (i == 0) {
                controlShortCut.max();
                llMaxContainer.addView(controlShortCut);
            } else {
                controlShortCut.min();
                if (i % 2 == 0) {
                    col2.addView(controlShortCut);
                } else {
                    col1.addView(controlShortCut);
                }
            }
            controlShortCut.setOnClickListener(view -> {
                consumer.accept(shortCut);
            });

            cshortcuts.add(controlShortCut);
        }
    }

    public void updateMsgCount(int count) {
        for (int i = 0; i < cshortcuts.size(); i++) {
            ControlShortcut shortCut = cshortcuts.get(i);
            if(shortCut.showMsgCount) {
                shortCut.badge(count);
            }
        }
    }

    public void setOnShortCutClick(Consumer<ShortCut> c) {
        this.consumer = c;
    }

}
